package com.haierCamera.customapplication;

import android.content.Context;
import android.util.Log;


import androidx.multidex.MultiDex;

import com.haierCamera.customapplication.api.ApiService;
import com.haierCamera.customapplication.di.DaggerAppComponent;
import com.haierCamera.customapplication.ui.MainActivity;
import com.haierCamera.customapplication.ui.business.Business;
import com.haierCamera.customapplication.ui.log.BuglyCrashCallBack;
import com.haierCamera.customapplication.ui.user.activity.HZKLUserAboutActivity;
import com.haierCamera.customapplication.utils.DownLoadNotification;
import com.haierCamera.customapplication.utils.ForegroundCallbacks;
import com.haierCamera.customapplication.utils.SharedPreferencesUtils;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;
import com.komlin.msgpush.KomlinPushManager;
import com.komlin.msgpush.interf.KomlinPushRevicer;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Objects;

import javax.inject.Inject;

import dagger.Lazy;
import dagger.android.AndroidInjector;
import dagger.android.support.DaggerApplication;

public class CustomApplication extends DaggerApplication {

    @Inject
    ApiService apiService;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    @Inject
    BuglyCrashCallBack crashCallBack;
    @Inject
    DownLoadNotification downLoadListener;

    @Override
    public void onCreate() {
        SharedPreferencesUtils.init(this);
        super.onCreate();
        Business.getInstance().init("lc6a11f13994a64f64", "48fa3b2aa58b462eac2444113befcb", "openapi.lechange.cn:443");
//        SharedPreferencesUtils.saveString(Constants.SP_USER_TOKEN, "Ut_0a0b854a184d40958781816a3a6ba563");
//        SharedPreferencesUtils.remove(Constants.SP_USER_TOKEN);
        //前后台切换监听
        ForegroundCallbacks.init(this);
        ForegroundCallbacks.get().addListener(new ForegroundCallbacks.Listener() {
            @Override
            public void onBecameForeground() {
                Log.i("TAG", "commonedition程序切换到前台");
            }

            @Override
            public void onBecameBackground() {
                Log.i("TAG", "commonedition程序切换到后台");

            }
        });
        KomlinPushManager.getInstance().initKomlinPushMsg(this, new KomlinPushRevicer() {
            @Override
            public void onToken(String token) {
                Log.i("TAG11", "Token:" + token + "----------1");
                SharedPreferencesUtils.saveString(Constants.SP_CID, token);
            }

            @Override
            public void onMsg(String msg) {
                Log.i("TAG11", "Token:" + msg + "----------2");
            }

            @Override
            public void onBadge(int num) {
                // BadgeUtil.setBadgeCount(getApplicationContext(), num, R.mipmap.ble_ic_launcher);
            }
        });
        initBugly();
        //initWeiXi();
    }

    private void initWeiXi() {
        IWXAPI api = WXAPIFactory.createWXAPI(this, Constants.WeiXiID, false);
        api.registerApp(Constants.WeiXiID);
    }

    void initBugly() {
        Beta.enableHotfix = false;
        Beta.defaultBannerId = R.drawable.hzkl_logo;
        Beta.canShowUpgradeActs.add(MainActivity.class);
        Beta.canShowUpgradeActs.add(HZKLUserAboutActivity.class);
        Beta.enableNotification = true;
        Beta.autoCheckUpgrade = true;
        Beta.upgradeCheckPeriod = 3 * 1000;
        if (Objects.equals(BuildConfig.BUILD_TYPE, "beta")) {
            Beta.autoDownloadOnWifi = true;
        }
        if (BuildConfig.DEBUG) {
            Beta.showInterruptedStrategy = true;
        }
        Beta.canShowApkInfo = true;
        Beta.downloadListener = downLoadListener;
        //Beta.tipsDialogLayoutId = R.layout.tips_dialog;
        //Beta.upgradeDialogLayoutId = R.layout.upgrade_dialog;
        CrashReport.UserStrategy userStrategy = new CrashReport.UserStrategy(this);
        userStrategy.setAppChannel(BuildConfig.BUILD_TYPE);
        userStrategy.setCrashHandleCallback(crashCallBack);
        Bugly.setIsDevelopmentDevice(this, BuildConfig.DEBUG);
        Bugly.init(this, "7dc9875b6e", BuildConfig.DEBUG, userStrategy);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);

    }

    @Override
    protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
        return DaggerAppComponent.builder().application(this).build();
    }


}
